<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    function test_Handler_Box_constructor(t) {
        t.plan(5);
        var control = new OpenLayers.Control();
        control.id = Math.random();
        var callbacks = {start: "foo", done: "bar"};
        var options = {bar: "foo"};
        
        var handler = new OpenLayers.Handler.Box(control, callbacks, options);

        t.eq(handler.control.id, control.id, "handler created with the correct control");
        t.eq(handler.callbacks.start, "foo", "handler created with the correct start callback");
        t.eq(handler.callbacks.done, "bar", "handler created with the correct done callback");
        t.eq(handler.bar, "foo", "handler created with the correct options");
        t.ok(handler.dragHandler instanceof OpenLayers.Handler.Drag, "drag handler created");
    }

    function test_Handler_Box_draw(t) {

        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control();
        map.addControl(control);
        var handler = new OpenLayers.Handler.Box(control, {
            start: function(e) {
                t.ok(true, "start callback called");
            },
            done: function(e) {
                t.ok(e.equals(new OpenLayers.Bounds(5, 11, 11, 5)), "box result correct");
            }
        });
        handler.activate();

        // determine whether we can test the box position, the hidden frame
        // our tests run in causes us problem here in FF and IE:
        // IE8: left is NaN
        // FF3: left is NaN
        // FF4; left is NaN
        // Chromium 10: left is 0
        var testdiv = OpenLayers.Util.createDiv('testdiv', new OpenLayers.Pixel(5, 5));
        map.div.appendChild(testdiv);
        var left = parseInt(OpenLayers.Element.getStyle(testdiv, 'border-left-width'));
        map.div.removeChild(testdiv);
        var testAll = !isNaN(left);

        t.plan(testAll ? 11 : 3);

        // we change NaN values to 0 values in the handler's
        // boxOffsets object, this is to prevent "invalid
        // "argument" errors in IE
        if(!testAll) {
            var offset = handler.getBoxOffsets();
            offset.left = 0;
            offset.right = 0;
            offset.top = 0;
            offset.bottom = 0;
            offset.width = 0;
            offset.height = 0;
        }


        handler.dragHandler.start = {x: 5, y: 5};
        handler.startBox();
        offset = handler.getBoxOffsets();
        handler.moveBox({x: 10, y: 10});
        if (testAll) {
            t.eq(parseInt(handler.zoomBox.style.left), 5 - offset.left, "x position of box correct");
            t.eq(parseInt(handler.zoomBox.style.top), 5 - offset.top, "y position of box correct");
            t.eq(parseInt(handler.zoomBox.style.width), 5 + offset.width + 1, "x dimension of box correct");
            t.eq(parseInt(handler.zoomBox.style.height), 5 + offset.height + 1, "y dimension of box correct");
        }
        handler.moveBox({x: 0, y: 0});
        if (testAll) {
            t.eq(parseInt(handler.zoomBox.style.left), 0 - offset.left, "new x position of box correct");
            t.eq(parseInt(handler.zoomBox.style.top), 0 - offset.top, "new y position of box correct");
            t.eq(parseInt(handler.zoomBox.style.width), 5 + offset.width + 1, "x dimension of box still correct");
            t.eq(parseInt(handler.zoomBox.style.height), 5 + offset.height + 1, "y dimension of box still correct");
        }
        handler.endBox({x: 11, y: 11});
        t.eq(handler.zoomBox, null, "box removed after endBox");
    }
    
    function test_Handler_Box_destroy(t) {
        t.plan(1);
        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control();
        map.addControl(control);
        var handler = new OpenLayers.Handler.Box(control);
        handler.activate();
        try {
            handler.destroy();
            t.ok(true, "destroying the box handler should not raise any error");
        } catch(err) {
            t.fail("destroying the box handler causes trouble: " + err);
        }
        map.destroy();
    }

  </script>
</head>
<body>
    <div id="map" style="width: 300px; height: 150px;"></div>
</body>
</html>
